﻿using System;

namespace DDDSample.Domain.Cargo
{
    /// <summary>
    /// 货运
    /// </summary>
    public class Cargo
    {
        protected Cargo()
        {
        }

        public Cargo(RouteSpecification routeSpecification)
        {
            if (routeSpecification == null)
            {
                throw new ArgumentNullException("routeSpecification");
            }
            RouteSpecification = routeSpecification;
            Delivery = Delivery.DerivedFrom(RouteSpecification, Itinerary, null);
        }

        public virtual Guid Id { get; protected set; }

        public virtual Delivery Delivery { get; set; }

        public virtual Itinerary Itinerary { get; set; }

        public virtual RouteSpecification RouteSpecification { get; set; }

        public virtual void AssignToRoute(Itinerary itinerary)
        {
            if (itinerary == null)
            {
                throw new ArgumentNullException("itinerary");
            }
            //TODO: Handle raise event
            //CargoHasBeenAssignedToRouteEvent @event = new CargoHasBeenAssignedToRouteEvent(this, Itinerary);
            Itinerary = itinerary;
            Delivery = Delivery.UpdateOnRouting(RouteSpecification, Itinerary);
            //DomainEvents.Raise(@event);
        }

        public virtual void SpecifyNewRoute(RouteSpecification routeSpecification)
        {
            if (routeSpecification == null)
            {
                throw new ArgumentNullException("routeSpecification");
            }
            RouteSpecification = routeSpecification;
            Delivery = Delivery.UpdateOnRouting(RouteSpecification, Itinerary);
        }
    }
}